

var curPage = 1;
var pageSize = 20;

var checkStatus = ['未审核', '已审核', '被否决']
var wanghongShow = {
    getList: function() {
        $.ajax({
            url: window.domain + window.requestUrl.getPlatInfoUrl,
            type: 'get',
            data: {
                pageSize: pageSize,
                platId: 2
            },
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function(res) {
                if(typeof(res.not_login) != 'undefined') {
                    window.setCookie('go_back_link', window.location.href);
                    window.location.href = './login.html';
                    return;
                }
                if(res.status) {
                    var pager = res.data.result;
                    wanghongShow.initPaging(pager.pages, pager.total, res);
                } else {
                    iosOverlay({
                        text: res.msg,
                        duration: 1000
                    });
                }
            },
            error: function(e) {
            }
        })
    },
    initPaging: function(totalPage, totalDataNum, res) {
        var self = this,
            ele = $('[data-role="dirPaging"]');
            if(totalDataNum > 0) {
                ele.show();
                var pagination = ele.pagination(totalDataNum, {
                    num_edge_entries: 1,
                    num_display_entries: 4,
                    callback: function(page_index) {
                        if(page_index == 0) {
                            var totalPage = res.data.result.pages;

                            var pageData = res.data.result.list;
                            var result = res.data.result;
                            wanghongShow.randerList(pageData);
                        } else {
                            curPage = page_index + 1;
                            wanghongShow.getPageDates();
                        }
                    },
                    items_per_page: pageSize,
                    prev_text: "&lt;&lt;上一页",
                    next_text: "下一页&gt;&gt;",
                    link_to: "javascript:;"
                });

            }else {
                ele.hide();
                $('[data-role="result"]').html('<tr><td  colspan="7" style="text-align: center;line-height: 100px;font-size: 18px;">没有数据</td></tr>')
            }
    },
    getPageDates: function() {
        var self = this;

        $.ajax({
            url: window.domain + window.requestUrl.getPlatInfoUrl,
            type: 'get',
            data: {
                pageNum: curPage,
                pageSize: pageSize,
                platId: 2
            },
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function(res) {
                if(typeof(res.not_login) != 'undefined') {
                    window.setCookie('go_back_link', window.location.href);
                    window.location.href = './login.html';
                    return;
                }

                // 同步页码
                var totalPage = res.data.result.pages;

                var pageData = res.data.result.list;
                var result = res.data.result;
                self.randerList(pageData);
            },
            error: function() {
                // console.log('分页接口失败');
            }
        });
    },
    randerList: function(pageData) {
        var tpl = '';

        $.each(pageData, function(index, item) {
            var controlsTpl = '';
            var hrefTpl = '';
            if(item.checkStatus == 1) {
                controlsTpl = '<div class="tr-control">\
                                    <a href="./wanghong-edit.html?id='+item.id+'" data-role="edit">编辑</a>\
                                    <a href="javascript:;" data-role="del" data-id="'+item.id+'">删除</a>\
                                </div>';
                hrefTpl = './wanghong-detail.html?id=' + item.id;
            } else if(item.checkStatus == 2) {
                controlsTpl = '<div class="tr-control">\
                                    <a href="javascript:;" class="ext-disable">编辑</a>\
                                    <a href="javascript:;" data-role="del" data-id="'+item.id+'">删除</a>\
                                </div>';
                hrefTpl = 'javascript:;';
            } else {
                controlsTpl = '<div class="tr-control">\
                                    <a href="javascript:;" class="ext-disable">编辑</a>\
                                    <a href="javascript:;" class="ext-disable">删除</a>\
                                </div>';
                hrefTpl = 'javascript:;';
            }
            tpl += '<tr>\
                        <td>\
                            <div class="tr-main">\
                                <a href="'+hrefTpl+'" target="_blank">\
                                    <img src="'+item.iconUrl+'" alt="'+item.title+'">\
                                </a>\
                                <div class="tr-main__name">\
                                    <a href="'+hrefTpl+'" target="_blank">\
                                        <p style="line-height:50px;">'+item.title+'</p>\
                                    </a>\
                                </div>\
                            </div>\
                        </td>\
                        <td>'+item.fans+'</td>\
                        <td>YY</td>\
                        <td>'+((JSON.parse(item.prices).price1 == -1) ? '无' : JSON.parse(item.prices).price1)+'</td>\
                        <td>'+((JSON.parse(item.prices).price2 == -1) ? '无' : JSON.parse(item.prices).price2)+'</td>\
                        <td>'+checkStatus[item.checkStatus]+'</td>\
                        <td>'+controlsTpl+'</td>\
                    </tr>';
        })
        $('[data-role="result"]').html(tpl);
    },
    init: function() {
        this.getList();
    }
}

wanghongShow.init();
